// pages/me/me.js
const app=getApp()
const db=wx.cloud.database()
Page({
  data: {
    isLogin: false,
    userInfo: {},
    show: true,
    showPupup: false,
    pupList: false,
    exp: 0,
    point: 0,
    time: 0,
    times: 0,
    pubTimes: 0,
    tabbar: {},
    showClockIn:false,
    level:1
  },
  exe(){
    app.checkIsSign(app.globalData.userInfo.userInfo,"../me_all/userSignIn/userSignIn")
     wx.navigateTo({
       url: '../me_all/exeDetail/exeDetail',
     })
  },
  exeGet(){
    app.checkIsSign(app.globalData.userInfo.userInfo,"../me_all/userSignIn/userSignIn")
    wx.navigateTo({
      url: '../me_all/exeGet/exeGet',
    })
  },
  Aboutus(){
    wx.navigateTo({
      url: '../me_all/Aboutus/Aboutus',
    })
  },
  myActivity(){
   var a= app.checkIsSign(app.globalData.userInfo.userInfo,"../me_all/userSignIn/userSignIn")
     if(a)
     {
      wx.navigateTo({
        url: '../me_all/MyActivity/MyActivity',
      })
     }
    
  },
  myHonor(){
    var a= app.checkIsSign(app.globalData.userInfo.userInfo,"../me_all/userSignIn/userSignIn")
    if(a)
    {
      wx.navigateTo({
        url: '../me_all/MyHonor/MyHonor',
      })
    }
  },
  goPointDetail(){
    var a= app.checkIsSign(app.globalData.userInfo.userInfo,"../me_all/userSignIn/userSignIn")
    if(a)
    {
      wx.navigateTo({
        url: '../me_all/pointDetail/pointDetail',
      })
    }
  },
  getUserInfoStro(){
    var UserInfoStro=wx.getStorageSync("user")
    console.log("UserInfoStro",UserInfoStro)
    if(UserInfoStro!="")
    this.setData({
      userInfo:UserInfoStro,
      isLogin:true
    })
    
  },
  clockIn11(){
    wx.navigateTo({
      url: '../PubTextPic/PubTextPic',
    })
  },
  login() {
      wx.getUserProfile({
      desc: '用于获取用户信息',
      success: (res) => {
        let user=res.userInfo
        console.log("用户信息", res)
        wx.cloud.callFunction({
          name:"updateUserInfo",
          data:{
              action:"userInfo",
              avaUrl:res.userInfo.avatarUrl,
              openid:app.globalData.openid
          }
        })
        this.setData({
          userInfo: res.userInfo,
          isLogin: true
        })
        app.saveUserInfo(user);
        this.getUserInfo()
      }
    })
  },
  showPupup() {
    this.setData({
      showPupup: true
    })
  },
  updateUserInfo(){
    wx.navigateTo({
      url: '../me_all/updateUserInfo/updateUserInfo',
    })
  },
  //关闭成长清单弹窗
  onClose() {
    this.setData({
      showPupup: false,
      pupList: false
    })
  },
  goExeGet(){
   wx.navigateTo({
     url: '../exeGet/exeGet',
   })
  },
  //展示成长清单
  pupList() {
    this.setData({
      pupList: true
    })
  },
  goOrder() {
    wx.navigateTo({
      url: '../Rights/Rights',
    })
  },
  //登陆打卡
  clockIn(){
    console.log("用户的_id",app.globalData.userInfo._id)
    let _id=app.globalData.userInfo._id
    var todayYear = (new Date()).getFullYear();
    var todayMonth = (new Date()).getMonth()+1;
    var todayDay = (new Date()).getDate();
    let todayLastTime = (new Date(todayYear, todayMonth, todayDay, '23', '59', '59','999')).getTime(); //今天最后一秒的时间戳
    console.log("今天最后一秒的时间戳",todayLastTime);

    let now=new Date().getTime()
    console.log("现在的时间戳",now)
    wx.cloud.database().collection('all_user').doc(_id)
    .get()
    .then(res=>{
      console.log(res)
      console.log("获取最后一次打卡的时间戳成功",res.data.clockInTime)

      if (res.data.clockInTime < now) {
        wx.cloud.database().collection('all_user').doc(_id)
            .update({
                data:{
                    clockInTime:todayLastTime
                }
            })
            .then(res=>{
                console.log("修改成功",res)
                this.setData({
                  showClockIn:true,
                  clockIn:true,
                })
            })
            .catch(res=>{
                console.log("修改失败",res)
            })
      }

      else{
        this.setData({
          showClockIn:true,
          clockIn:false
        })
      }
    })
    .catch(err=>{
      console.log("获取最后一次打卡的时间戳失败",err)
    })
  },

  closeClockIn(){
    this.setData({
      showClockIn:false
    })
  },
  lottery(){
     wx.navigateTo({
       url: '../me_all/prizeDetail/prizeDetail',
     })
  },
  notsign()
  {
    const that=this 
    console.log("用户退出登录")
    wx.showModal({
      title: '确定要退出登陆吗',
     
      success (res) {
      if (res.confirm) {
      console.log('用户点击确定')
      
      that.setData({
        isLogin:false,
        point: 0,
        time: 0,
        times: 0,
        pubTimes: 0
      })
      
      } else if (res.cancel) {
      console.log('用户点击取消')
      }
      }
      })
  },
  getUserInfo()
  {
    console.log("获取信息")
    db.collection('all_user')
    .where({
     _openid:app.globalData.openid
    })
    .get()
    .then(res=>{
      console.log("我的页用户信息",res)
       var  pointList=res.data[0].point
       var exeList=res.data[0].exe
       for(var j=0;j<exeList.length;j++)
       {
         console.log("aa",exeList[j].exeDetail)
          this.data.exe+=exeList[j].exeDetail
       }
       for(var i=0;i<pointList.length;i++)
       {
          this.data.point+=pointList[i].pointDetail
       }
       
        console.log("aaa",this.data.exe)
        this.setData({
          point:this.data.point,
          exe:this.data.exe,
          times:res.data[0].has_be_admit.length,
        })
    })
  },
  updateAPPuserInfo()
  {
     db.collection('all_user')
     .where({
       _openid:app.globalData.openid
     })
     .get()
     .then(res=>{
       console.log("res",res.data[0])
       app.globalData.userInfo=res.data[0]
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    setInterval(() => {
      app.checkIsSign(app.globalData.userInfo.userInfo,"../me_all/userSignIn/userSignIn")
  }, 21000)
  wx.hideTabBar()
    app.editTabbar()
    this.getUserInfoStro()
    this.getUserInfo()
    this.updateAPPuserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideTabBar()
    app.editTabbar()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})